package com.zxy.barometer.common.config;

import com.xiaoleilu.hutool.crypto.SecureUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

/**
 * Swagger2 配置文件
 *
 * @author zxy
 */
@Configuration
@EnableSwagger2
@ConditionalOnExpression("${swagger.show}")
public class SwaggerConfig {
    /**
     * swagger2的配置文件，这里可以配置swagger2的一些基本的内容，比如扫描的包等等
     *
     * @return 配置信息
     */
    @Bean
    public Docket createRestApi() {
        //添加head参数start
        List<Parameter> pars = new ArrayList<>();
        ParameterBuilder tokenPar = new ParameterBuilder();
        tokenPar.name("token").description("token").modelRef(new ModelRef("string")).parameterType("header").defaultValue("TOKEN_BUS_b06045fc1307e818703f303c50831537").required(false).build();
        ParameterBuilder requestIdPar = new ParameterBuilder();
        String requestId = String.valueOf(System.currentTimeMillis());
        requestIdPar.name("requestId").description("流水号").modelRef(new ModelRef("string")).parameterType("header").defaultValue(requestId).required(true).build();
        ParameterBuilder channelNoPar = new ParameterBuilder();
        String channelNo = "XN";
        channelNoPar.name("channelNo").description("渠道号(XN=西宁,BT=包头)").modelRef(new ModelRef("string")).parameterType("header").defaultValue(channelNo).required(true).build();
        ParameterBuilder timeStampPar = new ParameterBuilder();
        String timeStamp = String.valueOf(System.currentTimeMillis());
        timeStampPar.name("timeStamp").description("时间戳").modelRef(new ModelRef("string")).parameterType("header").defaultValue(timeStamp).required(true).build();
        ParameterBuilder versionPar = new ParameterBuilder();
        String version = "v1";
        versionPar.name("version").description("版本号").modelRef(new ModelRef("string")).parameterType("header").defaultValue(version).required(true).build();
        ParameterBuilder hmacPar = new ParameterBuilder();
        hmacPar.name("hmac").description("加密码").modelRef(new ModelRef("string")).parameterType("header").defaultValue(SecureUtil.md5("requestId=" + requestId + "&channelNo=" + channelNo + "&timeStamp=" + timeStamp + "&version=" + version + "we73hg3").toUpperCase()).required(true).build();
        pars.add(tokenPar.build());
        pars.add(requestIdPar.build());
        pars.add(channelNoPar.build());
        pars.add(timeStampPar.build());
        pars.add(hmacPar.build());
        pars.add(versionPar.build());
        //添加head参数end

        Predicate<RequestHandler> predicate = input -> {
            //只有添加了ApiOperation注解的method才在API中显示
            if (input != null) {
                return input.isAnnotatedWith(ApiOperation.class);
            } else {
                return false;
            }
        };

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(predicate::test)
                //为当前包路径
//                .apis(RequestHandlerSelectors.basePackage("com.heyue.busappserver.controller"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);
    }

    /**
     * 构建 api文档的详细信息函数,注意这里的注解引用的是哪个
     *
     * @return api文档信息
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("天气接口")
                //创建人
                .contact(new Contact("周徐阳", "http://www.baidu.com", ""))
                //版本号
                .version("1.0")
                //描述
                .description("API 描述")
                .build();
    }
}
